package com.ibuonline.b2b.product.dao;

import java.util.List;

import com.dimeng.common.base.SuperDAO;
import com.ibuonline.b2b.entity.ProdCategory;
import com.ibuonline.b2b.entity.ProdCategoryHierarchy;

public class CategoryDAO extends SuperDAO<ProdCategory> {
	
	private static final String HQL_GET_CAT_HIERARCHY = "select h " +
			"from ProdCategory c, ProdCategoryHierarchy h " +
			"where c.id = h.catId order by h.parentId, h.displayOrder";
	
	private static final String HQL_UPDATE_CAT_HIERARCHY = "update ProdCategoryHierarchy h set h.displayOrder=? where h.catId=? and h.parentId=?";
	
	@SuppressWarnings("unchecked")
	public List<ProdCategoryHierarchy> getAllCategoryHierarchy() {
		return template.find(HQL_GET_CAT_HIERARCHY);
	}
	
	public void updateProdCategory(ProdCategory prodCategory) {
		save(prodCategory);
		template.bulkUpdate(HQL_UPDATE_CAT_HIERARCHY, 
				new Object[]{prodCategory.getDisplayOrder(), prodCategory.getId(), prodCategory.getParentId()});
	}
}
